/**
 * 定义DataAnalysisController数据分析模块
 */
(function(window, angular, undefined){
	var waterQualityAnalysisManage = angular.module('waterQualityAnalysisManage', ['ui.router','itfreer','waterCommon']);
	
	waterQualityAnalysisManage.controller('WQAController',function($scope,$waterDataCommon,$DataReportService,OperationStationJobService,$httpData,$queryObj,$wdp,$rootScope,$state,$DataAnalysisService,$q){
		
		$scope.mulitPollInitCount = 0; //参数初始化计数器，用于自动触发查询，当计数器的值大于
		$scope.RiverBasinComData = [];
		$scope.RiverComData = [];
		$scope.StationComData = [];
		$scope.StationSectionData = [];
		$scope.contriStation = [];
		$scope.PollComData = [];
		$scope.PollToCharComData = [];
		$scope.PollMulitComData = {};
		$scope.sewageStation=[];
		$scope.RiverBasinSelectCodes = [];
		$scope.RiverSelectCode = [];
		$scope.RiverSelectCodePollCou = [];//通量分析河流
		$scope.RiverPartsSelectCode = [];
		$scope.stationLocationSelectCodes = [];
		$scope.dateLength = 4;
		$scope.m2my2yType = "M2M";//默认环比
		$scope.cycleType = "month";//默认月周期
		$scope.startDate = "2017-05-20";
		$scope.endDate = "2017-05-26";
		$scope.PollSelectCode = [];
		$scope.MulitStationPollSelectCode = {};
		$scope.analysisType = "";
		$scope.StationPollCouSelectCodes=[];
		$scope.StationSelectCodes = [];
		$scope.StationTributeSelectCodes =[];	
		$scope.StationSelectCodesGX=[];
		$scope.SewageSelectCodesGX=[];
		$scope.pollReductAnalysisData={};
		$scope.echar = [];//当前展示的图表
		$scope.cycleTypeComData = [{"k":"day","v":"日"},{"k":"week","v":"周"},{"k":"month","v":"月"},{"k":"quarter","v":"季"},{"k":"year","v":"年"}];
		$scope.riverPartComData = [{"k":"2","v":"中游"},{"k":"1","v":"上游"},{"k":"3","v":"下游"}];
		$scope.stationLocationComData = [{"k":"1","v":"河流"},{"k":"2","v":"水库"},{"k":"3","v":"湖泊"}];
		$scope.m2my2yTypeComData = [{"k":"M2M","v":"环比"},{"k":"Y2Y","v":"同比"}];
		$scope.analysisTypeComData = [{"k":"trend","v":"趋势分析"},
		                              {"k":"M2MY2Y","v":"同比环比分析"},
		                              {"k":"waterType","v":"水质综合分析"},
		                              {"k":"pollCou","v":"污染物通量分析"}
		                             ];
		$scope.analysisTypeComData1 = [{"k":"trend","v":"趋势分析"},
		                              {"k":"M2MY2Y","v":"同比环比分析"},
		                              {"k":"waterType","v":"水质综合分析"}];
		$scope.reductMethodData = [{"k":"percent","v":"比例分析法"}
		                             ];
		$scope.excuteStandardData = [ {"k":"LV5","v":"V类"},
		                              {"k":"LV4","v":"IV类"},
		                              {"k":"LV3","v":"III类"}
		                             ];
		
		$("#listdata").idTabs();
		$("#m2my2ylistdata").idTabs();	
		$("#pollCoulistdata").idTabs();
		$("#waterTypeData").idTabs();
		$("#waterTypeCharTab").idTabs();
		
		/**
		 * 加载流域
		 */
		$scope.queryRiverBasin = function(){
			$waterDataCommon.getPageRows("/BaseInfo/queryRiverBasin.do","RiverBasinList",{},1,1,function(data){
				$scope.RiverBasinSelectCodes.push(data[0].RIVER_BASIN_CODE);//默认选中第一条
				$scope.queryRiver($scope.RiverBasinSelectCodes);//查询河流
				$scope.RiverBasinComData=[];
				for(var i=0;i<data.length;i++){
					var item = data[i];
					$scope.RiverBasinComData.push({k:item.RIVER_BASIN_CODE,v:item.RIVER_BASIN_NAME});
				}
			});
		}
		
		/**
		 * 流域选择事件
		 */
		$scope.onRiverBasinChange = function(riverBasins){
			$scope.queryRiver(riverBasins);
		}
		
		/**
		 * 根据流域加载河流
		 */
		$scope.queryRiver = function(riverBasinCodes){
			var riverBasinCodesStr = riverBasinCodes.join(",");
			$waterDataCommon.getPageRows("/BaseInfo/queryRiver.do","RiverBasinList",{riverBasinCodes:riverBasinCodesStr},1,1,function(data){
				$scope.RiverComData.splice(0,$scope.RiverComData.length);
				for(var i=0;i<data.length;i++){
					var item = data[i];
					$scope.RiverComData.push({k:item.RIVER_CODE,v:item.RIVER_NAME});
				}
			});
		}
		
		/**
		 * 河段河流选择事件
		 */
		$scope.onRiverChange = function(){	
			var riverPart=$scope.RiverPartsSelectCode.join(",");
			var riverCodes = "";
			if($scope.analysisType=='pollCou'||$scope.analysisType=='pollReduct'){
				riverCodes = $scope.RiverSelectCodePollCou;	
			}else{
				riverCodes = $scope.RiverSelectCode.join(",");
			}
			$scope.queryStation(riverPart,riverCodes);
		}
		
		/**
		 * stationLocation选择事件
		 */
		$scope.onStationLocationChange = function() {	
			if($scope.stationLocationSelectCodes=='1') {
				$scope.RiverBasinComData = [];
				$scope.queryRiverBasin();
			} else {
				$scope.RiverBasinComData=[];
				$scope.queryStationLocationStation($scope.stationLocationSelectCodes);
			}
		}
		
		
		/**
		 * 通量分析通量数据合计
		 */
		$scope.parameterTotal=function(parameterList,SumData,conStationList){
			var amount=0;
			if(parameterList!=undefined){
				for(var i=0;i<parameterList.length;i++){
					var parname="A"+parameterList[i].S_DOMAINCODE;
					amount=amount+SumData[parname]+conStationList[parname];
				}
			}
			return amount;
		}
		/**
		 * 根据河流和河段加载站点
		 */
		$scope.queryStation = function(riverPart,riverCode){
			var waterTypes="";
			var type= getWaterType();
			waterTypes+=type;
			//增加对污水处理厂站点的查询，主要是用于通量分析中，该通量要包含污水处理厂的流量。
			waterTypes=waterTypes+",0";
			var queryObj = {
				riverPart:riverPart,
				riverCodes:riverCode,
				BUILD_STATUS:"0,-1",
				waterType:waterTypes
			};
			$waterDataCommon.getPageRows("/StationInfo/queryStationListForSelect.do","stationList",queryObj,1,1,function(data){
				$scope.StationComData.splice(0,$scope.StationComData.length);	
				$scope.StationSectionData.splice(0,$scope.StationSectionData.length);	
				if(data.length == 0){
					$scope.clearData();
					$scope.StationSelectCodes = [];
					$scope.StationPollCouSelectCodes={};
					$scope.contriStation=[];
					$scope.sewageStation=[];
					$scope.PollMulitComData=[];
					$scope.MulitStationPollSelectCode={};
					$scope.pollReductAnalysisData={};
					return;
				}
				for(var i=0;i<data.length;i++){
					var item = data[i];
					$scope.StationComData.push({k:item.CODE,v:item.NAME,w:item.RIVER_SECTION});
				}
				
				queryObj.STATION_ID=data[0].CODE;				
				queryObj.riverCode=data[0].RIVER_CODE
			});
		}
		/**
		 * 查询贡献站点
		 */
		$scope.ContributeStation = function(queryObj){
			var deferred = $q.defer();
			var promise = deferred.promise;
			$waterDataCommon.getPageRows("/StationInfo/queryContributeStation.do","stationList",queryObj,1,1,function(data1){
				$scope.contriStation.splice(0,$scope.contriStation.length);
				if(data1.contribute.length>0){
					for(var i=0;i<data1.contribute.length;i++){
						var item = data1.contribute[i];
						$scope.contriStation.push({k:item.CODE,v:item.NAME,w:item.RIVER_SECTION});
					}
				} else {
					$scope.contriStation=[];
					$scope.StationSelectCodesGX=[];
				}	
				$scope.sewageStation=[];
				if(data1.sewageResult.length>0){
					for(var i=0;i<data1.sewageResult.length;i++){
						var item = data1.sewageResult[i];
						$scope.sewageStation.push({k:item.CODE,v:item.NAME,w:item.RIVER_SECTION});
					}
				} else {
					$scope.sewageStation=[];
					$scope.SewageSelectCodesGX=[];
				}
				deferred.resolve({data:data1});
			});
			return promise;
		}
		/**
		 * 站点选择改变事件
		 */
		$scope.onStationChange = function(stationCodes){
			$("#M2MY2YTD span.datebox").css("float","left");
			$scope.queryPoll(stationCodes);
		}
		
		/**
		 * 通量分析的时候，根据贡献断面查询参数
		 */
		$scope.onStationPollCouChangeSN = function(){
			
			if($scope.analysisType!="pollCou"&&$scope.analysisType!="pollReduct"){
				return;
			}
			
			var queryObj={};
			//受纳站点code
			queryObj.STATION_ID=$scope.StationPollCouSelectCodes;
			queryObj.riverCodes=$scope.RiverSelectCodePollCou;
			 queryObj.riverPart="";
			 for(var i=0;i<$scope.RiverPartsSelectCode.length;i++){
				 if($scope.RiverPartsSelectCode.length-i==1){
					 queryObj.riverPart+="'"+$scope.RiverPartsSelectCode[i]+"'";
				 }else{
					 queryObj.riverPart+="'"+$scope.RiverPartsSelectCode[i]+"',";
				 }
				
			 }
			$scope.contriStation=[];
			$scope.getPollAndContributeStation(queryObj).then(function(){
				$scope.queryAnalysis();
			});
			
		}
		/**
		 * 获取污染物和贡献站点
		 */
		$scope.getPollAndContributeStation = function(queryObj){
			var deferred = $q.defer();
			var promise = deferred.promise;
			var readyCount = 0;
			$scope.queryPoll([queryObj.STATION_ID]).then(function(){
				readyCount++;
				if(readyCount == 2){
					deferred.resolve();
				}
			});
			$scope.ContributeStation(queryObj).then(function(){
				readyCount++;
				if(readyCount == 2){
					deferred.resolve();
				}
			});
			return promise;
		}
		
		/**
		 * 贡献站点选择事件
		 */
		$scope.onGXStationChange = function(){
			var promise = $scope.queryAnalysis();
		}
		
		/**
		 * 分析项选择事件
		 */
		$scope.onAnalysisTypeChange = function(data){
			if(data == "pollCou"||data == "pollReduct"){
				$scope.onStationPollCouChangeSN();
			}
			else{
				$scope.queryPoll($scope.StationSelectCodes);
			}
		}
		/**
		 * 根据站点查询参数
		 */
		$scope.queryPoll = function(stationCodes){
			var deferred = $q.defer();
			var promise = deferred.promise;
			
			var stationCodesStr = stationCodes.join(",");
			$waterDataCommon.getPageRows("/PollInfo/getMulitStationPollDict.do","pollList",{STATION_ID:stationCodesStr},1,1,function(data){
				$scope.mulitPollInitCount = 0; //重新获取参数，计数器要置空
				$scope.PollMulitComData = [];
				$scope.MulitStationPollSelectCode={};
				if(!angular.equals({}, data)) {
					for(var item in data){
						var curPollComData = [];
						var pollList = data[item];
						var stationCode = pollList[0].STATION_CODE;
						$scope.MulitStationPollSelectCode[stationCode] = [];
						for(var i=0;i<pollList.length;i++){
							var pollItem = pollList[i];
							curPollComData.push({"k":pollItem.CODE,"v":pollItem.NAME,"stationName":item,"STATION_CODE":pollItem.STATION_CODE});
						}
						curPollComData.push({"k":"COMP_POLL_INDEX","v":"综合污染指数"});
						$scope.PollMulitComData.push(curPollComData);
					}
				} else {
					$scope.MulitStationPollSelectCode={};
				}
				deferred.resolve({data:data});
			});
			return promise;
		}
		
		/**
		 * 参数选择后事件,防止在初始化参数的过程中就触发查询，这里要做个判断，没有初始化完不能查询
		 */
		$scope.onPollChange = function(data){
			$scope.mulitPollInitCount++;
			if($scope.mulitPollInitCount >= $scope.PollMulitComData.length){
				$scope.queryAnalysis();
			}
		}
		
		/**
		 * 点击对比列表显示出环比，同比时间。
		 */
		$scope.waterTypeSubType="fenxi";
		$scope.BTType="false";
		/**
		 * 污染物选择事件
		 */
		$scope.getTime = function(){
			$scope.waterTypeSubType='duibi';
			$scope.BTType="false";
		}
		$scope.cancelTime = function(){
			$scope.waterTypeSubType='fenxi';
			$scope.BTType="false";
		}
		$scope.cancelTimeAndBTType = function(){
			$scope.waterTypeSubType='fenxi';
			$scope.BTType="true";
		}
		
		/**
		 * 清空查询数据
		 */
		$scope.clearData = function(){
			$scope.analysisData = null;
			$scope.TypeData=null;
			$scope.analysisM2MY2YData = null;
			$scope.pollCouAnalysisData = null;
		}
		
		/**
		 * 检查参数是否完整
		 */
		function checkParmReady(){
			var pollParma = true;
			
			for(var stationCode in $scope.MulitStationPollSelectCode){
				var pollSelectItem = $scope.MulitStationPollSelectCode[stationCode];
				if(pollSelectItem.length < 1 ){
					pollParma = false;
					break;
				}
			}
			if($scope.StationSelectCodes.length < 1 ||
					!pollParma){
				return false;
			}
			else{
				return true;
			}
		}
		
		/**
		 * 查询分析
		 */
		$scope.queryAnalysis = function(){
			
			$scope.searchPanelHeight = parseInt($("#searchPanel").css("height"));
			$scope.layout();
			
			if(!checkParmReady()){
				return;
			}
			
			var start = new Date($scope.startDate);
			var end = new Date($scope.endDate);
			
			var queryObj = {
				stationid:$scope.StationSelectCodes.join(","),
				pollCode:getMulitPollSelectCode($scope.MulitStationPollSelectCode),//$scope.PollSelectCode.join(","),
				riverCode:$scope.RiverSelectCode.join(","),
				contributeCode:$scope.StationPollCouSelectCodes,//基准断面站点
				analysisType:$scope.analysisType,
				cycleType:$scope.cycleType,
				startDate:$scope.startDate,
				endDate:$scope.endDate,
				m2my2yType:$scope.m2my2yType,
				dateLength:$scope.dateLength				
			};
			
			 if(queryObj.analysisType=="trend") {
				 $scope.trendAnalysisIsShow=true;
				 $scope.waterTypeIsShow=false;
				$waterDataCommon.getPageRows("/waterQualityAnalysis/queryTrend.do","stationList",queryObj,1,1,function(data){
					$scope.analysisData = data;
					if(data.stations.length == 1){
						$scope.showTrendChar(data,'charDiv','line',"shadow");
					}
					else{
						$scope.showTrendChar(data,'charDiv','line');
					}
					$scope.showTrendChar(data,'charHistogram1','bar');
				});
			 } else if (queryObj.analysisType=="waterType") {
				 
				 if($scope.PollSelectCode=='COMP_POLL_INDEX') {
					 $scope.PollSelectCode = [$scope.PollComData[1].k];
					 queryObj.pollCode=$scope.PollComData[1].k;
				 }
				 $scope.trendAnalysisIsShow=false;
				 $scope.waterTypeIsShow=true;
				 $scope.BingTU=false;
					$waterDataCommon.getPageRows("/waterQualityAnalysis/queryWaterType.do","waterTypeData",queryObj,1,1,function(data){
						
						if(data==null) {
							$scope.TypeData=null;
						}else {
							$scope.TypeData= data.data[0];
							$scope.Y2YM2MDate = data.data[1];
						}
						//综合分析分析列表对应的饼图
						$DataAnalysisService.DrawBingTuWaterTypeChart($scope.TypeData,'waterType');	
						$DataAnalysisService.DrawBingTuCompPollIndexChart($scope.TypeData,'compPollIndex');
						//综合分析对比列表中 综合污染指数变化饼图
						$DataAnalysisService.DrawBingTuCompPollIndexChangeChart($scope.Y2YM2MDate,'COMP_ANALYSIS_CHART');
						//河段饼图
						//河段水质类别饼图
						$DataAnalysisService.DrawRiverSectionWaterTypeChart($scope.TypeData,'riverSectionWaterType');
						//河段水质状况饼图
						$DataAnalysisService.DrawRiverSectionCompPollIndexChart($scope.TypeData,'riverSectionCompPollIndex');	
					});
			 }
			 else if(queryObj.analysisType=="M2MY2Y"){
				 $waterDataCommon.getPageRows("/waterQualityAnalysis/queryM2MY2Y.do","M2MY2YData",queryObj,1,1,function(data){
					 $scope.analysisM2MY2YData = data;
					 $scope.showM2MY2YCharByPoll(data,"m2my2yChar_poll");
				});
				//获取地表水通量分析数据
			 }else if(queryObj.analysisType=="pollCou"){
				 queryObj.stationid=$scope.StationSelectCodesGX.join(",")+","+$scope.SewageSelectCodesGX.join(",");
				 queryObj.riverCodePollcou=$scope.RiverSelectCodePollCou;
				 queryObj.RiverPartsPollcou="";
				 for(var i=0;i<$scope.RiverPartsSelectCode.length;i++){
					 if($scope.RiverPartsSelectCode.length-i==1){
						 queryObj.RiverPartsPollcou+="'"+$scope.RiverPartsSelectCode[i]+"'";
					 }else{
						 queryObj.RiverPartsPollcou+="'"+$scope.RiverPartsSelectCode[i]+"',";
					 }
				 }
				 $waterDataCommon.getPageRows("/waterQualityAnalysis/queryFlux.do","stationPollCouList",queryObj,1,1,function(data){
						$scope.pollCouAnalysisData = data;
						$scope.PollToCharComData.splice(0,$scope.PollToCharComData.length);
						for(var p=0;p<data.conData.polls.length;p++){
							$scope.PollToCharComData.push({"k":data.conData.polls[p].POLLCODE,"v":data.conData.polls[p].POLLNAME});
						}
					});
			 } else if(queryObj.analysisType=="pollReduct"){
				 //贡献站点
				 queryObj.GXStationid=$scope.StationSelectCodesGX.join(",")+","+$scope.SewageSelectCodesGX.join(",");
				 //河流
				 queryObj.riverCodePollcou=$scope.RiverSelectCodePollCou;
				 //削减方案
				 queryObj.reductMethod=$scope.reductMethod;
				 //执行标准
				 queryObj.excuteStandard=$scope.excuteStandard;
				 //河段
				 //受纳站点加河段
				 var SNStationSection=queryObj.contributeCode;
				 for(var j=0;j<$scope.StationComData.length;j++) {
					 var stationData = $scope.StationComData[j].k;
					 if(SNStationSection==stationData) {
						 queryObj.contributeCode=queryObj.contributeCode+":"+$scope.StationComData[j].w+",";
					 }
				 }
				 //贡献站点加河段
				 var stationAndSection=[];
				 stationAndSection=queryObj.GXStationid.split(",");
				 var stationSect="";
				 for(var i=0;i<stationAndSection.length;i++) {
					 var data=stationAndSection[i];
					 for(var j=0;j<$scope.contriStation.length;j++) {
						 var stationData = $scope.contriStation[j].k;
						 if(data==stationData) {
							 stationSect+=data+":"+$scope.contriStation[j].w+",";
						 }
					 }
					 for(var j=0;j<$scope.sewageStation.length;j++) {
						 var stationData = $scope.sewageStation[j].k;
						 if(data==stationData) {
							 stationSect+=data+":"+$scope.sewageStation[j].w+",";
						 }
					 }
				 }
				 stationSect = stationSect.substring(0,stationSect.length-1);
				 queryObj.GXStationid = stationSect;
				 
				 queryObj.RiverPartsPollcou="";
				 for(var i=0;i<$scope.RiverPartsSelectCode.length;i++){
					 if($scope.RiverPartsSelectCode.length-i==1){
						 queryObj.RiverPartsPollcou+="'"+$scope.RiverPartsSelectCode[i]+"'";
					 }else{
						 queryObj.RiverPartsPollcou+="'"+$scope.RiverPartsSelectCode[i]+"',";
					 }
				 }
				 $waterDataCommon.getPageRows("/waterQualityAnalysis/queryPollReduct.do","stationPollReductList",queryObj,1,1,function(data){
						$scope.pollReductAnalysisData = data;
						$scope.dateText=[];
						if($scope.pollReductAnalysisData.SNReductData) {
							$scope.dateText = $scope.pollReductAnalysisData.SNReductData.dateTextList;
						} else if ($scope.pollReductAnalysisData.GXReductData){
							$scope.dateText = $scope.pollReductAnalysisData.GXReductData.dateTextList;
						}
					});
			 }
		}
		
		/**
		 * 获取多站点参数集合
		 */
		function getMulitPollSelectCode(data){
			var pollCodeStr = ""; 
			for(var stationCode in data){
				pollCodeStr += "@"+stationCode+":";
				var pollSelectItem = data[stationCode];
				for(var i=0;i<pollSelectItem.length;i++){
					pollCodeStr += pollSelectItem[i]+",";
				}
			}
			return pollCodeStr;
		}
		
		/**
		 * 选择参数画对应的通量饼图
		 */
		$scope.showCouChar = function(poll){
			$DataAnalysisService.DrawMulitCouBar($scope.pollCouAnalysisData,"pollCouChar",poll);
		}
		
		/**
		 * 画趋势曲线图
		 */
		$scope.showTrendChar = function(data,domid,charType,shadow){
			$scope.echar = [];
			$("#"+domid).empty();
			
			if(data&&data.pollDeduplicated) {
				for(var p=0;p<data.pollDeduplicated.length;p++){
					var charDiv = $("<div class='charDiv' id='"+domid+p+"'></div>").css({height: 300,width:$rootScope.Map_W});
					$("#"+domid).append(charDiv);
					var currentPollName = data.pollDeduplicated[p].POLLNAME;
					var legendNames = [];
					var yDatas = [];
					var xDatas = [];
					 
					for(var j=0;j<data.dateTexts.length;j++){
					 xDatas.push(data.dateTexts[j]);
					}
					
					for(var s = 0;s<data.stations.length;s++){
						var yDatasItem = [];//均值
						var ylDatasItem = [];//最小值
						var yuDatasItem = [];//最大值
						var currentMM = data.stations[s].MN;
						legendNames.push(data.stations[s].NAME);
						//这里要根据参数是否为“综合污染指数”进行区分，数据格式不一样，为综合污染指数的时候只有均值数据，否则就包括最大值，最小值，均值数据。
						if(currentPollName=="综合污染指数") {
							for(var i=0;i<data.data.length;i++){
								var pollName = data.data[i].PARMNAME_DESC;
								var mn = data.data[i].MN;
								if(currentPollName != pollName || currentMM != mn){
									continue;
								}
								for(var d=0;d<xDatas.length;d++){
										//如果是查的综合污染指数，只有均值，没有最大值最小值
									yDatasItem.push(data.data[i]['P'+xDatas[d]]);
								 }
							}
						} else {
							for(var i=0;i<data.data.length;i+=3){
								var pollName = data.data[i].PARMNAME_DESC;
								var mn = data.data[i].MN;
								if(currentPollName != pollName || currentMM != mn){
									continue;
								}
								for(var d=0;d<xDatas.length;d++){
									var value = parseFloat(data.data[i]['P'+xDatas[d]]);
									var max = parseFloat(data.data[i+1]['P'+xDatas[d]]);
									var min = parseFloat(data.data[i+2]['P'+xDatas[d]]);
									 yDatasItem.push(value);
									 yuDatasItem.push(max - min);
									 ylDatasItem.push(min);
								 }
							}	
						}
						if(shadow){
							yDatas.push(ylDatasItem);
							yDatas.push(yuDatasItem);
						}
						yDatas.push(yDatasItem);
					}
					var myChart = $waterDataCommon.showMulitLineChar(legendNames,xDatas,yDatas,domid+p,charType,{title:data.pollDeduplicated[p].POLLNAMESHOW,'shadow':shadow});
					$scope.echar.push(myChart);
				}
			}
		}
		
		/**
		 * 画同比环比柱状图,按站点分
		 */
		$scope.showM2MY2YCharByStation = function(data,domid){
			$("#"+domid).empty();
			
			 var xDatas = [];
			 var charType = "bar";
			 for(var i=0;i<data.dateTexts.length;i++){
				 xDatas.push(data.dateTexts[i]);
			 }
			
			for(var s = 0;s<data.stations.length;s++){
				var charDiv = $("<div class='charDiv' id='"+domid+s+"'></div>").css({height: 300,width:$rootScope.Map_W});
				$("#"+domid).append(charDiv);
				var legendNames = [];
				 var yDatas = [];
				
				 var stationDataStartIndex = (s) * data.polls.length;
				 var stationDataEndIndex = (s + 1) * data.polls.length;
				 
				 for(var i=0;i<data.polls.length;i++){
					 var currentPollName = data.polls[i].POLLNAME;
					 legendNames.push(data.polls[i].POLLNAMESHOW);
					 for(var j=stationDataStartIndex;j<stationDataEndIndex;j++){
						 var pollName = data.data[j].PARMNAME_DESC;
						 if(currentPollName === pollName){
							 var yDatasItem = [];
							 for(var d=0;d<xDatas.length;d++){
								 yDatasItem.push(data.data[j]['P'+xDatas[d]]);
							 }
							 yDatas.push(yDatasItem);
						 }
					 }
				 }
				 
				 $waterDataCommon.showMulitLineChar(legendNames,xDatas,yDatas,domid+s,charType,{title:data.stations[s].NAME});
			}
		}
		
		/**
		 * 画同比环比柱状图,按污染物分
		 */
		$scope.showM2MY2YCharByPoll = function(data,domid){
			$("#"+domid).empty();
			var xDatas = [];
			var charType = "bar";
			for(var i=0;i<data.dateTexts.length;i++){
				xDatas.push(data.dateTexts[i]);
			}
			
			for(var p=0;p<data.pollDeduplicated.length;p++){
				var charDiv = $("<div class='charDiv' id='"+domid+p+"'></div>").css({height: 300,width:xDatas.length*180});
				$("#"+domid).append(charDiv);
				var legendNames = [];
				var yDatas = [];
				var currentPollName = data.pollDeduplicated[p].POLLNAME;
			
				for(var s = 0;s<data.stations.length;s++){
					var yDatasItem = [];
					var currentMM = data.stations[s].MN;
					legendNames.push(data.stations[s].NAME);
					var stationDataStartIndex = (s) * data.polls.length;
					 var stationDataEndIndex = (s + 1) * data.polls.length;
					for(var i=0;i<data.data.length;i++){
						var pollName = data.data[i].PARMNAME_DESC;
						var mn = data.data[i].MN;
						if(currentPollName != pollName || currentMM != mn){
							continue;
						}
						for(var d=0;d<xDatas.length;d++){
							 yDatasItem.push(data.data[i]['P'+xDatas[d]]);
						 }
					}
					
					yDatas.push(yDatasItem);
				}
				$waterDataCommon.showMulitLineChar(legendNames,xDatas,yDatas,domid+p,charType,{title:data.pollDeduplicated[p].POLLNAMESHOW});
			}
		}
		$scope.dataHeight = $wdp.Map_H-300;
		$scope.dataWidth = $wdp.Map_W-20;
		$scope.showSearchPanel = function(){
			$scope.shousuo = false;
			$scope.layout();
		}
		
		$scope.hidenSearchPanel = function(){
			$scope.shousuo = true;
			$scope.layout();
		}
		
		$scope.layout = function(){
			if($scope.shousuo){
				$scope.dataHeight = $wdp.Map_H-65;
			}
			else{
				$scope.dataHeight = $wdp.Map_H-$scope.searchPanelHeight - 15;
			}
			window.echars = $scope.echar;
			setTimeout(function(){
				for(var i=0;i<$scope.echar.length;i++){
					$scope.echar[i].resize();
				}
            }, 500)
			
		}
		
		$scope.$on("onLeftPanelSwitch",function(){
			$scope.dataWidth = $wdp.Map_W-20;
			$scope.layout();
		});
		
		$scope.queryRiverBasin();
		
		/**
		 * stationLocation:1,2,3 ;如果为1：直接查询有哪些流域；如果不为1，直接查询水库和湖泊站点。
		 */
		$scope.queryStationLocationStation = function(stationLocation){
			$scope.StationComData = [];
			var waterTypes="";
			var type= getWaterType();
			waterTypes+=type;
			//增加对污水处理厂站点的查询，主要是用于通量分析中，该通量要包含污水处理厂的流量。
			waterTypes=waterTypes+",0";
			var queryObj = {
				stationLocation:stationLocation,
				BUILD_STATUS:"0,-1",
				waterType:waterTypes
			};
			$waterDataCommon.getPageRows("/StationInfo/queryStationListForSelect.do","stationList",queryObj,1,1,function(data){
				if(data.length == 0){
					$scope.clearData();
					$scope.StationSelectCodes = [];
					$scope.StationPollCouSelectCodes={};
					$scope.contriStation=[];
					$scope.sewageStation=[];
					$scope.PollMulitComData=[];
					$scope.MulitStationPollSelectCode={};
					$scope.pollReductAnalysisData={};
					return;
				}
				for(var i=0;i<data.length;i++){
					var item = data[i];
					$scope.StationComData.push({k:item.CODE,v:item.NAME,w:item.RIVER_SECTION});
				}
				
				queryObj.STATION_ID=data[0].CODE;				
				queryObj.riverCode=data[0].RIVER_CODE;
			});
		}
		$scope.exportTrendExcel = function() {
			var start = new Date($scope.startDate);
			var end = new Date($scope.endDate);
			
			var queryObj = {
				stationid:$scope.StationSelectCodes.join(","),
				pollCode:getMulitPollSelectCode($scope.MulitStationPollSelectCode),
				riverCode:$scope.RiverSelectCode.join(","),
				contributeCode:$scope.StationPollCouSelectCodes,//基准断面站点
				analysisType:$scope.analysisType,
				cycleType:$scope.cycleType,
				startDate:$scope.startDate,
				endDate:$scope.endDate,
				m2my2yType:$scope.m2my2yType,
				dateLength:$scope.dateLength				
			};
			
			 if($scope.PollSelectCode=='COMP_POLL_INDEX') {
				 $scope.PollSelectCode = [$scope.PollComData[1].k];
				 queryObj.pollCode=$scope.PollComData[1].k;
			 }
			 var queryString = $waterDataCommon.jsonTostring(queryObj);
			 window.open(getBasePath()+"/waterQualityAnalysis/exportTrendExcel.do?"+queryString);
		}
		
	});
}(window, angular));	